class Solution {
  // Same as 160. Intersection of Two Linked Lists
  public Node lowestCommonAncestor(Node p, Node q) {
    Node a = p;
    Node b = q;

    while (a != b) {
      a = a == null ? q : a.parent;
      b = b == null ? p : b.parent;
    }

    return a;
  }
}
